package com.sac.approval.service.impl;

import com.sac.approval.dao.ApprovalLineTemplateDao;
import com.sac.approval.dao.ApprovalTemplateDao;
import com.sac.approval.dao.AttachmentDao;
import com.sac.approval.domain.ApprovalLineTemplateDO;
import com.sac.approval.domain.ApprovalTemplateDO;
import com.sac.approval.domain.AttachmentDO;
import com.sac.approval.service.ApprovalTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;



@Service
public class ApprovalTemplateServiceImpl implements ApprovalTemplateService {
	@Autowired
	private ApprovalTemplateDao approvalTemplateDao;

	@Autowired
	private ApprovalLineTemplateDao approvalLineTemplateDao;

	@Autowired
	private AttachmentDao attachmentDao;
	
	@Override
	public ApprovalTemplateDO get(String appTemplateId){
		return approvalTemplateDao.get(appTemplateId);
	}
	
	@Override
	public List<ApprovalTemplateDO> list(Map<String, Object> map){
		return approvalTemplateDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return approvalTemplateDao.count(map);
	}
	
	@Override
	public int save(ApprovalTemplateDO approvalTemplate){
		return approvalTemplateDao.save(approvalTemplate);
	}
	
	@Override
	public int update(ApprovalTemplateDO approvalTemplate){
		return approvalTemplateDao.update(approvalTemplate);
	}
	
	@Override
	public int remove(String appTemplateId){
		return approvalTemplateDao.remove(appTemplateId);
	}
	
	@Override
	public int batchRemove(String[] appTemplateIds){
		return approvalTemplateDao.batchRemove(appTemplateIds);
	}

	@Override
	@Transactional
	public int saveApprovalTemplate(ApprovalTemplateDO approval, List<ApprovalLineTemplateDO> appLines, List<AttachmentDO> attachments) {
		int i = approvalTemplateDao.update(approval);
		approvalLineTemplateDao.removeByAppTemplateId(approval.getAppTemplateId());
		appLines.forEach(line-> {
			line.setAppTemplateId(approval.getAppTemplateId());
			approvalLineTemplateDao.save(line);
		});
		attachmentDao.removeByAppTemplateId(approval.getAppTemplateId());
		attachments.forEach(attachment-> {
			attachment.setAppTemplateId(approval.getAppTemplateId());
			attachmentDao.save(attachment);
		});

		return i;
	}

    @Override
    @Transactional
    public int updateApprovalTemplate(ApprovalTemplateDO approval, List<ApprovalLineTemplateDO> appLines, List<AttachmentDO> attachments) {
	    int i = approvalTemplateDao.update(approval);

	    int rm = approvalLineTemplateDao.removeByAppTemplateId(approval.getAppTemplateId());

        int rma = attachmentDao.removeByAppTemplateId(approval.getAppTemplateId());

		appLines.forEach(line-> {
			line.setAppTemplateId(approval.getAppTemplateId());
			approvalLineTemplateDao.save(line);
		});
		attachments.forEach(attachment-> {
			attachment.setAppTemplateId(approval.getAppTemplateId());
			attachmentDao.save(attachment);
		});

		return i;
    }

    @Override
	@Transactional
	public int batchUpdate(List<ApprovalTemplateDO> approvalTemplateList) {
		approvalTemplateList.forEach(s-> approvalTemplateDao.update(s));
		return approvalTemplateList.size();
	}

}
